/**
 * @description 产品控制器
 * @author 韩
 */

const { randomUUID } = require("crypto")
const model = require("../../../model/custManager/customerList/CUSTOMER.MODEL")
const { getFilter } = require("../../../utils/setFilter")
const dayjs = require("dayjs")
// 创建一个内容
const controller_save = async ({
    id = undefined,
    custName = undefined,
    custId = undefined,
    custEmail = undefined,
    phoneNo = undefined,
    sort = undefined,
    status = undefined


}) => {
    try {
        const params = {
            custName, custId, custEmail, phoneNo, sort, status
        }
        let addRes = null;
        if (!id) {
            // 添加创建
            addRes = await model.create({
                ...params,
                id: randomUUID(),
                createDate: dayjs().format("YYYY-MM-DD HH:mm:ss")
            })
        } else {
            // 更新修改
            addRes = await model.findOneAndUpdate({ id: id }, {
                $set: {
                    ...params
                }
            }, { new: true })
        }

        if (addRes) {
            const {
                id, custName, custId, custEmail, phoneNo, sort, status
            } = addRes



            return {
                code: 0,
                msg: "成功",
                data: {
                    custName, custId, custEmail, phoneNo, sort, status
                }
            }
        }
        return {
            code: 100,
            msg: "失败",
        }
    } catch (error) {
        return {
            code: 100,
            msg: "服务器故障",
        }
    }
}

// 查询列表 
const controller_find = async ({
    id = undefined,
    custName = undefined,
    custId = undefined,
    custEmail = undefined,
    phoneNo = undefined,
    status = undefined,
    current, pageSize,
}) => {

    try {
        let params = {
            id, custName, custId, custEmail, phoneNo, status
        }
        filter = getFilter(params)
        const list = await model.find(filter).sort({ sort: 1 }).skip((current - 1) * pageSize).limit(pageSize)
        const totalCount = await model.countDocuments(filter)
        return {
            code: 0,
            msg: "成功",
            data: {
                list,
                totalCount
            }
        }
        // /**
        //  * @description 聚合查询
        //  * 
        //  */
        // const queryList = await model.aggregate([
        //     {
        //         $match: {
        //             ...filter,
        //             deleteFlag: false
        //         }
        //     },
        //     {
        //         $lookup: {
        //             from: "main_tab_skus",
        //             localField: "id",
        //             foreignField: "productId",
        //             as: "skus"
        //         },
        //         $lookup: {
        //             from: "correlation_product_categories",
        //             localField: "categoryId",
        //             foreignField: "productId",
        //             as: "cateId"
        //         }
        //     }
        // ])
        // return queryList;
    } catch (error) {
        return {
            code: 100,
            msg: "服务器故障",
        }
    }
}


/**@description 根据id查详情 */
const controller_get = async ({ id, title }) => {
    try {
        const res = await model.findOne({
            id
        })
        if (res) {
            const { id, custName, custId, custEmail, phoneNo, sort, status } = res;
            return {
                code: 0,
                msg: "查询成功",
                data: {
                    id, custName, custId, custEmail, phoneNo, sort, status
                }
            }
        } else {
            return {
                code: 100,
                msg: "未找到对应数据"
            }
        }
    } catch (error) {
        return {
            code: 100,
            msg: "服务器故障",
        }
    }
}

// 删除
const controller_delete = async ({ ids = [], id = undefined }) => {
    try {
        //物理删除
        // const res = await model.deleteMany({
        //     id: { $in: ids }
        // });
        // 逻辑删除
        const res = await model.findOneAndUpdate({ id: id }, {
            $set: {
                deleteFlag: true
            }
        }, { new: true })

        if (res) {
            return {
                code: 0,
                msg: `删除成功`
            }
        } else {
            return {
                code: 100,
                msg: `删除失败`
            }
        }
    } catch (error) {
        console.log("删除控制器出错", error)
        return {
            code: 100,
            msg: "服务器故障",
        }
    }

}
module.exports = {
    controller_save,
    controller_find,
    controller_get,
    controller_delete,
}